package com.sugon.controller.tool;

import cn.afterturn.easypoi.entity.vo.TemplateExcelConstants;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.sugon.annotation.AutoLog;
import com.sugon.annotation.NonBusinessParamValid;
import com.sugon.entity.ToolsBaseEntity;
import com.sugon.modules.base.model.vo.BaseApproveParamVO;
import com.sugon.modules.excel.IExcelHandler;
import com.sugon.modules.tools.service.IToolsManager;
import com.sugon.service.SysConfigService;
import com.sugon.service.ToolsBaseService;
import com.sugon.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工装基础信息管理Controller
 *
 * @author YuXD
 * @date 2020-11-21 11:05
 */
@Controller
@RequestMapping("toolsbase")
public class ToolsBaseController {

    @Autowired
    private ToolsBaseService toolsBaseService;
    @Autowired
    private IToolsManager toolsManager;
    @Autowired
    private SysConfigService configService;
    @Autowired
    @Qualifier("toolsCheckJudgeStandardExcelHandler")
    private IExcelHandler excelHandler;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("toolsbase:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {

        //查询列表数据
        Query query = new Query(params);

        List<ToolsBaseEntity> toolsBaseList = toolsBaseService.queryList(query);
        int total = toolsBaseService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(toolsBaseList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("toolsbase:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        ToolsBaseEntity toolsBase = toolsBaseService.queryObject(id);
        return R.ok().put("toolsBase", toolsBase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("toolsbase:save")
    @ResponseBody
    public R save(@RequestBody ToolsBaseEntity toolsBase) {
        return toolsBaseService.save(toolsBase) > 0 ? R.ok() : R.error();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("toolsbase:update")
    @ResponseBody
    public R update(@RequestBody ToolsBaseEntity toolsBase) {
        return toolsBaseService.update(toolsBase) > 0 ? R.ok() : R.error();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("toolsbase:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        toolsBaseService.deleteBatch(ids);

        return R.ok();
    }

    @NonBusinessParamValid
    @RequestMapping("/review")
    @ResponseBody
    public R review(@Validated @RequestBody BaseApproveParamVO approveParam, BindingResult bindingResult) {
        approveParam.setProcessItem(ProcessInfoEnum.TLS_GZML);
        return toolsManager.workshopApprove(approveParam) ? R.ok() : R.error();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {
        List<ToolsBaseEntity> list = toolsBaseService.queryList(params);
        return R.ok().put("list", list);
    }

    @RequestMapping("/export")
    @RequiresPermissions("toolsbase:export")
    @ResponseBody
    public void export(@RequestParam Map<String, Object> params, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException {
        // 查询工装清单列表
        MapUtil.removeEmptyValue(params);
        List<ToolsBaseEntity> toolsBaseList = toolsBaseService.queryList(params);
        Map<String, Object> map = new HashMap<>(1);
        map.put("toolsBaseList", toolsBaseList);
        // 构建模板导出参数
        TemplateExportParams templateExportParams = new TemplateExportParams(configService.getPoiTemplatePath() + "toolsBaseIETemplate.xlsx");
        templateExportParams.setColForEach(true);
        modelMap.put(TemplateExcelConstants.FILE_NAME, "工装基础信息_" + com.sugon.modules.utils.DateUtil.today());
        modelMap.put(TemplateExcelConstants.PARAMS, templateExportParams);
        modelMap.put(TemplateExcelConstants.MAP_DATA, map);
        PoiBaseView.render(modelMap, request, response, TemplateExcelConstants.EASYPOI_TEMPLATE_EXCEL_VIEW);
    }

    /**
     * excel导入
     */
    @RequestMapping("/import")
    @RequiresPermissions("toolsbase:import")
    @ResponseBody
    public R importE(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        return R.ok("成功导入" + toolsBaseService.importE(file) + "条数据");
    }

    /**
     * 工装周期性验证标准
     */
    @RequestMapping("/importJudgeStandard")
    @RequiresPermissions("toolsbase:import")
    @ResponseBody
    public R importCheckResult(@RequestParam("file") MultipartFile file) throws Exception {
        if (file == null || file.isEmpty()) {
            return R.error("上传文件不能为空！");
        }
        int rows = excelHandler.importExcelFromStream(file.getInputStream());
        return R.ok("成功导入" + rows + "记录");
    }

}
